﻿using A.M.ETO;
using A.M.Storage.Application;
using AutoMapper;
using DotNetCore.CAP;
using J.M.Inventory.Application.IInventoryServices;
using J.M.Inventory.Domain.DTO;
using J.M.Inventory.Domain.Entity;
using J.M.Inventory.Infrastruecure.IInventoryRepository;
using J.M.Inventory.Infrastruecure.InventoryRepository;
using Public_Class_Library;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace J.M.Inventory.Application.InventoryServices
{
    /// <summary>
    /// 出库管理表服务层实现
    /// </summary>
    public class EnterwarehouesServices : BaseServices<enterwarehoues, string>, IEnterwarehouesServices
    {
        //出库表
        private readonly IEnterwarehouesRepository enterwarehouesRepository;
        //出库类型表
        private readonly IGo_warehouseRepository go_WarehouseRepository;
        //仓库表
        private readonly IWarehouseRepository warehouseRepository;
        private readonly IMapper mapper;
        ////跨服务
        private readonly Cross_service_Helper cross_Service_Helper;

        private readonly ICapPublisher capPublisher;
        public EnterwarehouesServices(IEnterwarehouesRepository enterwarehouesRepository, IMapper mapper, IGo_warehouseRepository go_WarehouseRepository, IWarehouseRepository warehouseRepository, Cross_service_Helper cross_Service_Helper, ICapPublisher capPublisher) : base(enterwarehouesRepository)
        {
            this.mapper = mapper;
            this.enterwarehouesRepository = enterwarehouesRepository;
            this.go_WarehouseRepository = go_WarehouseRepository;
            this.warehouseRepository = warehouseRepository;
            this.cross_Service_Helper = cross_Service_Helper;
            this.capPublisher = capPublisher;
        }
        /// <summary>
        /// 显示销售订单
        /// </summary>
        /// <param name="code"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public  PageETO<ListenterwarehouesDTO> GetEnterShow(string? code, int pageIndex, int pageSize)
        {
            PageETO<ListenterwarehouesDTO> pageETO = new PageETO<ListenterwarehouesDTO>();
            Expression<Func<enterwarehoues, bool>> exception = i => true;
            if (!string.IsNullOrEmpty(code))
            {
                exception = x => x.enterwarehousecode.Contains(code);
            }
            var enterwarehoues = enterwarehouesRepository.GetShowInfor(exception).ToList();

            var go_Warehouse = go_WarehouseRepository.GetShowInfor().ToList();

            var warehouse = warehouseRepository.GetShowInfor().ToList();

            var list = from a in enterwarehoues
                       join b in go_Warehouse on a.entertypeid equals b.gowarehouse_id
                       join c in warehouse on a.enterwearhouesid equals c.warehousecode
                       select new ListenterwarehouesDTO
                       {
                           clientname = a.clientname,
                           enterdatatime = a.enterdatatime,
                           enterfile = a.enterfile,
                           enterpersonnel = a.enterpersonnel,
                           entertheme = a.entertheme,
                           entertypeid = a.entertypeid,
                           enterwarehousecode = a.enterwarehousecode,
                           enterwearhouesid = a.enterwearhouesid,
                           examine_approve = a.examine_approve,
                           markdepartment = a.markdepartment,
                           marketcode = a.marketcode,
                           neckpersonnel = a.neckpersonnel,
                           productioncode = a.productioncode,
                           relevancyprojectcode = a.relevancyprojectcode,
                           remark = a.remark,
                           gowarehouse_name = b.gowarehouse_name,
                           warehousename = c.warehousename,
                       };
            pageETO.TotalCount = list.Count();
            pageETO.PageCount = (int)Math.Ceiling(pageETO.TotalCount / (double)pageSize);
            pageETO.Data = list.OrderByDescending(x => x.enterdatatime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageETO;
        }
        /// <summary>
        /// 添加销售订单
        /// </summary>
        /// <param name="listenterwarehouesDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiReturn<string>> GetSEnterAddInfo(enterwarehouesAddDTO  enterwarehouesAddDTO)
        {

            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var stock = mapper.Map<enterwarehoues>(enterwarehouesAddDTO);
            var res = await enterwarehouesRepository.AddInfor_Async(stock);
            if (res > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "添加成功";
                //添加成功需要传一个消息队列  传递哪个模块 还有编号
                string numberName = "出库系统-" + enterwarehouesAddDTO.enterwarehousecode;
                capPublisher.Publish("GetNewNumber", numberName);
                enterwarehouesRepository.CommitTranInfor();
            }
            return apiReturn;
        }
        /// <summary>
        /// 删除销售订单列表
        /// </summary>
        /// <param name="enterid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int DeleteEnter(string? enterid)
        {

            var list = enterwarehouesRepository.GetShowObj(x => x.enterwarehousecode == enterid);
            if (list == null)
            {
                return -1;
            }
            return enterwarehouesRepository.DelInfor(list);
        }
        /// <summary>
        /// 反填销售订单
        /// </summary>
        /// <param name="enterid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
       public async Task<enterwarehoues> FindEnterInfo(string? enterid)
        {
            Expression<Func<enterwarehoues, bool>> i = x => x.enterwarehousecode == enterid;
            var res = await enterwarehouesRepository.GetShowObjAsync(i);
            return res;
        }
        /// <summary>
        /// 查看仓库
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
       public async Task<ApiReturn<warehouse>>  GetShowwarehouse()
        {
            ApiReturn<warehouse> apiReturn = new ApiReturn<warehouse>();
            var lsit = await warehouseRepository.GetShowQueryableAsync();
            apiReturn.Code = 200;
            apiReturn.ListInfor = lsit.Select(x => new warehouse
            {
                warehousecode = x.warehousecode,
                warehousename = x.warehousename,
            }).ToList();
            return apiReturn;
        }
        /// <summary>
        /// 修改销售订单列表
        /// </summary>
        /// <param name="enterwarehouesAddDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
       public async Task<ApiReturn<string>> PostEnterUpdateInfo(enterwarehouesAddDTO enterwarehouesAddDTO)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var stock = mapper.Map<enterwarehoues>(enterwarehouesAddDTO);
            var res = enterwarehouesRepository.GetShowInfor(x => x.enterwarehousecode.Equals(enterwarehouesAddDTO.enterwarehousecode));

            var number = await enterwarehouesRepository.UpdInfor_Async(stock);
            if (number > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "修改成功";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "修改失败";
            }
            return apiReturn;
        }

        /// <summary>
        /// 销售订单的显示
        /// </summary>
        /// <param name="code"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        //public PageETO<ListenterwarehouesDTO> GetMarketShow(string? code, int pageIndex, int pageSize)
        //{

        //    //PageETO<ListenterwarehouesDTO> pageETO = new PageETO<ListenterwarehouesDTO>();
        //    //Expression<Func<enterwarehoues, bool>> exception = i => true;
        //    //if (!string.IsNullOrEmpty(code))
        //    //{
        //    //    exception = x => x.enterwarehousecode.Contains(code);
        //    //}
        //    //var enterwarehoues = enterwarehouesRepository.GetShowInfor(exception).ToList();

        //    //var go_Warehouse = go_WarehouseRepository.GetShowInfor().ToList();

        //    //var warehouse = warehouseRepository.GetShowInfor().ToList();

        //    //var list = from a in enterwarehoues
        //    //           join b in go_Warehouse on a.entertypeid equals b.gowarehouse_id
        //    //           join c in warehouse on a.enterwearhouesid equals c.warehousecode
        //    //           select new ListenterwarehouesDTO
        //    //           {
        //    //               clientname = a.clientname,
        //    //               enterdatatime = a.enterdatatime,
        //    //               enterfile = a.enterfile,
        //    //               enterpersonnel = a.enterpersonnel,
        //    //               entertheme = a.entertheme,
        //    //               entertypeid = a.entertypeid,
        //    //               enterwarehousecode = a.enterwarehousecode,
        //    //               enterwearhouesid = a.enterwearhouesid,
        //    //               examine_approve = a.examineapprove,
        //    //               markdepartment = a.markdepartment,
        //    //               marketcode = a.marketcode,
        //    //               neckpersonnel = a.neckpersonnel,
        //    //               productioncode = a.productioncode,
        //    //               relevancyprojectcode = a.relevancyprojectcode,
        //    //               remark = a.remark,
        //    //               gowarehouse_name = b.gowarehouse_name,
        //    //               warehousename = c.warehousename,
        //    //           };
        //    //pageETO.TotalCount = list.Count();
        //    //pageETO.PageCount = (int)Math.Ceiling(pageETO.TotalCount / (double)pageSize);
        //    //pageETO.Data = list.OrderBy(x => x.enter_warehouseid).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        //    //return pageETO;
        //}
    }
}
